package com.dhq.myapp.ui.menu

import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.dhq.myapp.R
import com.dhq.myapp.base.BaseActivity
import com.dhq.myapp.ui.home.HomeFragment

/**
 * DESC
 * Created by douhaoqiang on 2025/10/16.
 */

//非左右滑动页面与底部导航栏
class FragmentActivity : BaseActivity() {

    private lateinit var fragments: Map<Int, Fragment>
    private var viewPager: ViewPager? =null;

    override fun getLayoutId(): Int {
        return R.layout.activity_menu_lay
    }

    override fun initView() {
        viewPager = findViewById<ViewPager>(R.id.view_pager)
    }

    override fun initData() {
//        viewPager.visibility = View.GONE
//        fragments = mapOf(
//            R.id.fragment1 to createFragment(HomeFragment::class.java),
//            R.id.fragment2 to createFragment(HomeFragment::class.java),
//            R.id.fragment3 to createFragment(HomeFragment::class.java),
//            R.id.fragment4 to createFragment(HomeFragment::class.java)
//        )
//        bottomNavigationView.run {
//            setOnNavigationItemSelectedListener { menuItem ->
//                showFragment(menuItem.itemId)
//                true
//            }
//            setOnNavigationItemReselectedListener { menuItem ->
//                val fragment = fragments.entries.find {
//                    it.key == menuItem.itemId
//                }?.value
//                if (fragment is ScrollToTop) {
//                    fragment.scrollToTop()
//                }
//            }
//        }

        showBadgeView(2, 99)

//        if (savedInstanceState == null) {
//            val initialItemId = R.id.fragment1
//            bottomNavigationView.selectedItemId = initialItemId
//            showFragment(initialItemId)
//        }

    }



//    private fun createFragment(clazz: Class<out Fragment>): Fragment {
//        var fragment = supportFragmentManager.fragments.find { it.javaClass == clazz }
//        if (fragment == null) {
//            fragment = when (clazz) {
//                HomeFragment::class.java -> HomeFragment.newInstance()
//                HomeFragment::class.java -> HomeFragment.newInstance()
//                HomeFragment::class.java -> HomeFragment.newInstance()
//                HomeFragment::class.java -> HomeFragment.newInstance()
//                else ->
//                    throw IllegalArgumentException("argument ${clazz.simpleName} is illegal")
//            }
//        }
//        return fragment
//    }

    private fun showFragment(menuItemId: Int) {
        val currentFragment = supportFragmentManager.fragments.find {
            it.isVisible && it in fragments.values
        }
        val targetFragment = fragments.entries.find {
            it.key == menuItemId
        }?.value
        supportFragmentManager.beginTransaction().apply {
            currentFragment?.let {
                if (it.isVisible)
                    hide(it)
            }
            targetFragment?.let {
                if (it.isAdded)
                    show(it)
                else
                    add(R.id.fl, it)
            }
        }.commit()
    }

    /**
     * BottomNavigationView显示角标
     * @param viewIndex tab索引
     * @param showNumber 显示的数字，小于等于0是将不显示
     */
    private fun showBadgeView(viewIndex: Int, showNumber: Int) {

//        bottomNavigationView.getOrCreateBadge(R.id.fragment3).apply {
////            backgroundColor = ContextCompat.getColor(this@FragmentActivity, R.color.red)
////            badgeTextColor = ContextCompat.getColor(this@FragmentActivity, R.color.white)
////            number = showNumber
//        }

//        val menuItemId = bottomNavigationView.menu.getItem(viewIndex).itemId
//        val badge : BadgeDrawable= bottomNavigationView.getOrCreateBadge(menuItemId)
//        badge.number = showNumber
    }

}